package cn.app.web.controller;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import cn.app.entity.AppCategory;
import cn.app.entity.AppInfo;
import cn.app.entity.DataDictionary;
import cn.app.entity.DevUser;
import cn.app.service.AppCategoryService;
import cn.app.service.AppInfoService;
import cn.app.service.DataDictionaryService;
import cn.app.utils.PageBean;

import com.mysql.jdbc.StringUtils;

@Controller
@RequestMapping("/devApp")
public class DevAppController {
	
	@Autowired
	private DataDictionaryService dataDictionaryService;
	
	@Autowired
	private AppCategoryService appCategoryService;
	
	@Autowired
	private AppInfoService appInfoService;
	
	@RequestMapping("/disMain")
	public String disMain(){
		return "dev/main";
	}
	
	
	@RequestMapping("/appInfoList")
	public String appInfoList(Model model,
							  @RequestParam(value="softwareName",required=false)String softwareName,
							  @RequestParam(value="appStatus",required=false)String status,
							  @RequestParam(value="appFlatform",required=false)String flatform,
							  @RequestParam(value="appLevelOne",required=false)String levelOne,
							  @RequestParam(value="appLevelTwo",required=false)String levelTwo,
							  @RequestParam(value="appLevelThree",required=false)String levelThree,
							  @RequestParam(value="currentPage",required=false)Integer currentPage,
						      @RequestParam(value="pageSize",required=false)Integer pageSize){
		//currentPage
		
		//4.数据回显
		Map<String,Object> map = new HashMap<>();
		map.put("softwareName", softwareName=softwareName==null?"":softwareName.trim());
		map.put("status", status);
		map.put("flatform", flatform);
		map.put("levelOne", levelOne);
		map.put("levelTwo", levelTwo);
		map.put("levelThree", levelThree);
		map.put("currentPage", currentPage);
		map.put("pageSize", pageSize);
		
		//4.放到model中
		model.addAttribute("map", map);
		
		//5.查询指定的二级分类和三级分类,返回给jsp页面(回显数据)
		if(!StringUtils.isNullOrEmpty(levelOne)){
			List<AppCategory> levelTwoList = appCategoryService.findAllByPraent(levelOne);
			//5.1放到modle中
			model.addAttribute("levelTwoList", levelTwoList);
		}
		
		if(!StringUtils.isNullOrEmpty(levelTwo)){
			List<AppCategory> levelThreeList = appCategoryService.findAllByPraent(levelTwo);
			//5.1放到modle中
			model.addAttribute("levelThreeList", levelThreeList);
		}
		
		
		//1.查询App状态
		List<DataDictionary> statusList = dataDictionaryService.findAll("APP_STATUS");
		
		//1.放到model中
		model.addAttribute("statusList", statusList);
		
		//2.查询所属平台
		List<DataDictionary> flatformList = dataDictionaryService.findAll("APP_FLATFORM");
		
		//2.放到model中
		model.addAttribute("flatformList", flatformList);
		
		
		//3.查询一级分类
		List<AppCategory> levelOneList = appCategoryService.findAllByPraent(null);
		
		//3.放到model中
		model.addAttribute("levelOneList", levelOneList);
		
		//6.查询app全部信息
		PageBean<AppInfo> pageBean = appInfoService.findAllByCondition(map);
		
		//6.放到model中
		model.addAttribute("pageBean", pageBean);
		
		return "dev/appInfoList";
	}
	
	
	@RequestMapping("/findLevelJson")
	public @ResponseBody List<AppCategory> findLevelJson(@RequestBody Map<String,String> map){
		
		List<AppCategory> levelOneList = appCategoryService.findAllByPraent(map.get("levelId"));
		
		return levelOneList;
	}
	
	//跳转到新增app基础信息页面
	@RequestMapping("/disAppInfoAdd")
	public String disAppInfoAdd(Model model){
		
		//2.查询所属平台
		List<DataDictionary> flatformList = dataDictionaryService.findAll("APP_FLATFORM");
		
		//2.放到model中
		model.addAttribute("flatformList", flatformList);
		
		//3.查询一级分类
		List<AppCategory> levelOneList = appCategoryService.findAllByPraent(null);
		
		//3.放到model中
		model.addAttribute("levelOneList", levelOneList);
		
		return "dev/appInfoAdd";
	}
	
	//异步校验APK名称
	@RequestMapping("/checkAPKName")
	public @ResponseBody Map<String,String> checkAPKName(@RequestBody Map<String,String> map){
		//获得到APK名称
		String apkName = map.get("apkName");
		//调用service去数据库查询
		Integer count = appInfoService.findAppInfoByAPKName(apkName);
		Map<String,String> resultMap = new HashMap<>();
		if(count > 0){
			resultMap.put("apkNameInfo", "已经存在....");
		}
		return resultMap;
	}
	
	
	//新增APP基础信息
	@RequestMapping("/appInfoAdd")
	public String appInfoAdd(AppInfo appInfo,MultipartFile picFile,Model model,HttpSession session) throws Exception{
		/*if(!(picFile.getSize() > (50 * 1024))){
			//图片太大,应该小于50kb
		}*/
		if(picFile.getSize() > 0){
			//获得原名字
			String originalName = picFile.getOriginalFilename();
			//校验图片必须是.jpg/.png/.jpeg
			String typeName = originalName.substring(originalName.lastIndexOf("."));
			//判断
			if(!(".jpg".equals(typeName) ||".png".equals(typeName) ||".jpeg".equals(typeName))){
				//图片格式不正确
				model.addAttribute("picInfo", "图片格式不正确");
				return "forward:/devApp/disAppInfoAdd";
			}
			//保存图片
			//设置新名字
			String newName = UUID.randomUUID().toString() + typeName;
			//保存/转移
			picFile.transferTo(new File("E:\\pic\\" + newName));
			
			appInfo.setLogopicpath("http://localhost:8080/pic/" + newName);
			appInfo.setLogolocpath("E:\\pic\\" + newName);
		}
		DevUser devUser = (DevUser) session.getAttribute("devUser");
		appInfo.setDevid(devUser.getId());
		appInfo.setCreatedby(devUser.getId());
		Integer count = appInfoService.addAppInfo(appInfo);
		if(count > 0){
			//添加成功
			return "redirect:/devApp/appInfoList";
		}else{
			//添加失败
			model.addAttribute("addInfo", "添加失败,系统正在更新,请联系管理员");
			return "forward:/devApp/disAppInfoAdd";
		}
	}
	
	
	//跳转修改页面
	@RequestMapping("/disAppInfoUpdate/{id}")
	public String disAppInfoUpdate(@PathVariable("id")Integer id,Model model){
		//2.查询所属平台
		List<DataDictionary> flatformList = dataDictionaryService.findAll("APP_FLATFORM");
		
		//2.放到model中
		model.addAttribute("flatformList", flatformList);
		
		//3.查询一级分类
		List<AppCategory> levelOneList = appCategoryService.findAllByPraent(null);
		
		//3.放到model中
		model.addAttribute("levelOneList", levelOneList);	
		
		//去数据库中查询出当前app信息
		AppInfo appInfo = appInfoService.findAppInfoById(id);
		model.addAttribute("appInfo", appInfo);
		
		//5.查询指定的二级分类和三级分类,返回给jsp页面(回显数据)
		List<AppCategory> levelTwoList = appCategoryService.findAllByPraent(appInfo.getCategorylevel1());
		//5.1放到modle中
		model.addAttribute("levelTwoList", levelTwoList);
		List<AppCategory> levelThreeList = appCategoryService.findAllByPraent(appInfo.getCategorylevel2());
		//5.1放到modle中
		model.addAttribute("levelThreeList", levelThreeList);
			
		return "dev/appInfoUpdate";
	}
	
	
}
